package com.huiyuanai.service.user;

import com.huiyuanai.dao.User;
import com.huiyuanai.pojo.user.UserPojo;
import com.huiyuanai.pojo.user.UserPojoImpl;
import com.huiyuanai.util.BaseUtil;
import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.util.Date;
import java.util.List;

/**
 * @Author: liwan
 * @Description:
 * @Date: create in 2021/10/4/004
 */
public class UserServiceImpl implements UserService {

    //业务层会调用dao层
    private UserPojo userPojo = new UserPojoImpl();

    @Override
    public User logIn(String userCode) throws Exception {
        Connection connection = BaseUtil.getConnectionDIY();
        User user = userPojo.logIn(connection, userCode);

        //释放资源
        BaseUtil.clostResour(connection, null, null);

        return user;
    }

    @Override
    public int updataPWD(String pwd, int id) throws Exception {
        Connection connection = BaseUtil.getConnectionDIY();
        int i = userPojo.updataPWD(connection, pwd, id);

        //释放资源
        BaseUtil.clostResour(connection, null, null);
        return i;
    }

    @Override
    public int userCount(String userName, int role) throws Exception {
        Connection connection = BaseUtil.getConnectionDIY();
        int count = userPojo.userCount(connection, userName, role);
        //释放资源
        BaseUtil.clostResour(connection, null, null);
        return count;
    }

    @Override
    public List<User> getUserList(String userName, int role, int openPage, int countPage, String userCode) throws Exception {
        Connection connection = BaseUtil.getConnectionDIY();
        List<User> userList = userPojo.getUserList(connection, userName, role, openPage, countPage, userCode);
        //释放资源
        BaseUtil.clostResour(connection, null, null);
        return userList;
    }

    @Override
    public int addUser(User user) throws Exception {
        Connection connection = BaseUtil.getConnectionDIY();
        int result = userPojo.addUser(connection, user);
        return result;
    }

    @Override
    public int isUserCode(String userCode) throws Exception {
        Connection connection = BaseUtil.getConnectionDIY();
        int result = userPojo.isUserCode(connection, userCode);
        BaseUtil.clostResour(connection, null, null);
        return result;
    }

    @Override
    public int userDel(String userID) throws Exception {
        Connection connection = BaseUtil.getConnectionDIY();
        int result = userPojo.userDel(connection, userID);
        BaseUtil.clostResour(connection, null, null);
        return result;
    }

    @Override
    public int updataUser(User user) throws Exception {
        Connection connection = BaseUtil.getConnectionDIY();
        int result = userPojo.updataUser(connection, user);
        BaseUtil.clostResour(connection, null, null);
        return result;
    }

    @Test
    public void t() {
        UserService service = new UserServiceImpl();
        User user = new User();
        user.setId(18);
        user.setUserCode("liwan");
        user.setUserName("李万");
        user.setGender(1);
        user.setBirthday(new Date());
        user.setPhone("18727242529");
        user.setAddress("1111");
        user.setUserRole(1);
        try {
            int i = service.updataUser(user);
            System.out.println("这里" + i);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
